﻿var CouponsAPI = {
    Call: function(args) {
        var sParams = CouponsAPI.ParamstoString(args.Params);
        var DataType = (args.DataType) ? args.DataType : "json";

        $.ajax({
            type: "POST"
            , url: "CouponsWebService.asmx/" + args.Method
            , data: sParams
            , contentType: "application/json;"
            , dataType: DataType
            , success: args.OnComplete
            , failure: args.OnFail

        });
    },

    ParamstoString: function ParamstoString(Params) {
        var aParams = [], sParams = "";

        for (var Param in Params) {
            aParams.push(Param + ":\"" + Params[Param] + "\"");
        }

        sParams = "{" + aParams.join() + "}";
        return sParams
    }
}

function ShowLoading(type, El) {
    var X,Y,W,H;
    X=$(El).position().left;
    Y=$(El).position().top;
    W=$(El).width();
    H=$(El).height();
    var Class, Src;
    
    switch(type){
        case "small":
        	Src = "LoadingImg_small";
            break;

        case "big":
        	Src = "LoadingImg_big";
            break;
    }

    var div = document.createElement("div");
    //var img = new Image();
    div.setAttribute("id", "divLoading");
    div.setAttribute("class", "LoadingDiv");
    //img.setAttribute("class", "LoadingImg");
    //img.src = Src;

    $(div).css({ "left": X, "top": Y, "width": W, "height": H, "position": "absolute" });
    $(div).addClass(Src);
    //div.appendChild(img);
    $(El).parent().prepend(div);

}

function RemoveLoading() {
    $("#divLoading").each(function() { $(this).remove(); });
}

var fillCasinos = {
    Get: function(args) {
        ShowLoading("small", args.ShowLoadingEl);

        CouponsAPI.Call({
            Method: "GetCasinos"
            , Params: []
            , OnComplete: fillCasinos.Draw
            , OnFail: RemoveLoading()
        });
    },
    Draw: function(data) {
        var Casinos = eval("(" + data.d + ")");
        var CasinosHtml = [];

        CasinosHtml.push("<option value=''></option>");
        for (var x = 0; x < Casinos.length; x++) {
            CasinosHtml.push("<option value='" + Casinos[x].CasinoID + "'>" + Casinos[x].CasinoName + "</option>");
        }

        var sCasinosHtml = CasinosHtml.join("");
        var selCasinos = $(".selCasinos");
        for (var x = 0; x < selCasinos.length; x++) {
            $(selCasinos[x]).html(sCasinosHtml);
            var expand = selCasinos[x].getAttribute("expand");
            if (expand && expand == "true") $(selCasinos[x]).attr("size", CasinosHtml.length);
        }

        RemoveLoading();
    }
}

function IsDate(sDate) {
    try {
        var d = new Date(sDate).toDateString();
        
        return (d != "Invalid Date");
    }
    catch (e) {
        return false;
    }
}

function createEditCouponForm() {
	var input = "<span class='input'><label for='{for}'>{caption}:</label><input type='{type}' id='{id}'></span><br>";
	var button = "<button id='{id}' class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' role='button' aria-disabled='false'>"
		+ "<span class='ui-button-text'>{label}</span></button>"
	var html = [];
	html.push(
		"<div class='editCoupon' style='display:none'>"
			, input.replace("{for}", "editStartDate").replace("{id}", "editStartDate").replace("{caption}", "Start Date").replace("{type}","date")
			, input.replace("{for}", "editEndDate").replace("{id}", "editEndDate").replace("{caption}", "End Date").replace("{type}", "date")
			, input.replace("{for}", "editAmount").replace("{id}", "editAmount").replace("{caption}", "Amount").replace("{type}", "number")
			, button.replace("{id}", "btnEditCouponSave").replace("{label}", "Save")
			, button.replace("{id}", "btnEditCouponCancel").replace("{label}", "Cancel")
		, "</div>"
	);

	return html.join("");
			
}